home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 121_01 / zdir.c < prev    next >
Text File  |  1985-08-19  |  2KB  |  112 lines

  1. /*
  2. HEADER: CUG 121.??;
  3.  
  4.     TITLE:    Zdir - produce a directory listing;
  5.     VERSION:    1.0;
  6.     DATE:    12/01/85;
  7.     DESCRIPTION: "This program produces a directory list on the console,
  8.         with six directory entries per line.  Optionally, the
  9.         output may be sorted and/or written to a disk file as well."
  10.     KEYWORDS:    sorted, directory;
  11.     SYSTEM:    CP/M;
  12.     FILENAME:    ZDIR.C;
  13.     WARNINGS:    "Requires files.c for link. Handles a maximum of 128 files
  14.         (easily changed).";
  15.     SEE-ALSO:    SDIR.C (another directory lister);
  16.     AUTHORS:    Steve Blasingame;
  17.     COMPILERS:    BDS-C 1.50;
  18. */
  19.  
  20. /*    Flags:
  21.  *    -S     sort file names.
  22.  *    -F    place file names in file DIR.DIR.
  23.  */
  24.  
  25. #include <bdscio.h>
  26.  
  27. #define    MAXFILES 128    /* maximum # of filenames to store    */
  28.  
  29. char    fd[BUFSIZ];    /* i/o area    */
  30.  
  31.  
  32. main(argc,argv)
  33. char    *argv[];
  34. int    argc;
  35. {
  36.     char    *files[MAXFILES];
  37.     char    leftover;
  38.     char    fflag,sflag;        /* to sort or not to sort */
  39.     int    cmp();            /* declare here for qsort    */
  40.     int    i,j;
  41.  
  42.     _allocp = NULL;        /* initialize allocation pointer */
  43.     leftover = fflag = sflag = 0;
  44.     i = 1;            /* argument being processed */
  45.     while (argv[i][0] == '-')
  46.         {
  47.         for(j=1;argv[i][j];j++)
  48.             switch (argv[i][j]) {
  49.             case 'F':    /* place listing in DIR.DIR    */
  50.                 fflag=1;
  51.                 break;
  52.             case 'S':    /* sort file names        */
  53.                 sflag=1;
  54.                 break;
  55.             default:
  56.                 usage();
  57.                 exit();
  58.             }
  59.         i++;
  60.         }
  61.  
  62.     if (fflag && fcreat("DIR.DIR",fd)==ERROR) {
  63.         printf("Cannot create DIR.DIR\n");
  64.         exit();
  65.         }
  66.  
  67.     if (argc > i)
  68.         for(j=i;j<argc && leftover<MAXFILES-1;j++) {
  69.             files[leftover]=alloc(strlen(argv[j])+1);
  70.             strcpy(files[leftover++],argv[j]);
  71.             leftover=nameok(leftover,files,MAXFILES);
  72.             }
  73.     else {
  74.         files[leftover]=alloc(15);
  75.         strcpy(files[leftover++],"????????.???");
  76.         leftover=nameok(leftover,files,MAXFILES);
  77.         }
  78.  
  79.     if (sflag)
  80.         qsort(files,leftover,2,&cmp);
  81.  
  82.     if (!leftover) {
  83.         printf("No files to print.\n");
  84.         fclose(fd);
  85.         exit(0);
  86.         }
  87.  
  88.     if (fflag) {
  89.         for(j=0;j<leftover;j++)
  90.             fprintf(fd,"%s\n",files[j]);
  91.         putc(CPMEOF,fd);
  92.         fflush(fd);
  93.         fclose(fd);
  94.         }
  95.  
  96.     for (j=0; j<leftover; j++)
  97.         printf("%-12s%c",files[j],((j+1)%6)?' ':'\n');
  98.     putchar('\n');
  99.     printf("%d: Files. %s",leftover,(fflag)?"DIR.DIR done.\n":"\n");
  100. }
  101.  
  102. cmp(a,b)
  103. char    **a,**b;
  104. {
  105.     return(strcmp(*a,*b));
  106. }
  107.  
  108. usage()
  109. {
  110.     printf("Usage: zdir -[f][s] [<filename>] ...\n");
  111. }
  112.